﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4CodeSamples.Models.Entities;
using System.Data.Objects;
using PagedList.Mvc;
using PagedList;
namespace MVC4CodeSamples.Areas.DataAccess.Controllers
{
    public class ProductsController : Controller
    {
        private AdventureWorks2008Container db = new AdventureWorks2008Container();

        //
        // GET: /DataAccess/Products/

        public ActionResult Index(string SearchString)
        {

            if (string.IsNullOrEmpty(SearchString))
            {
                var products = db.Products.Include("ProductCategory").Include("ProductModel");
                return View(products.ToList());
            }
            else
            {
                var products = db.Products.Include("ProductCategory").Include("ProductModel");
                return View(products.Where(o => o.Name.Contains(SearchString)).ToList());
            }
           
        }

        public ActionResult PagedSearch(string SearchString, int page = 1)
        {
            ObjectQuery<Product> products = null;
            if (string.IsNullOrEmpty(SearchString))
            {
                 products = db.Products.Include("ProductCategory").Include("ProductModel");
               
            }
            else
            {
                 products = db.Products.Include("ProductCategory").Include("ProductModel");
                 products = (ObjectQuery<Product>)products.Where(o => o.Name.Contains(SearchString));
                
            }
            return View("Index",products.OrderBy(o=>o.Name).ToPagedList(page,10));
        }
        public ActionResult IndexSorted(string sOrder)
        {
           
            if (string.IsNullOrEmpty(sOrder))
            {
                var products = db.Products.Include("ProductCategory").Include("ProductModel");
                return View("Index", products.ToList());
            }
            else
            {
                var products = db.Products.Include("ProductCategory").Include("ProductModel");
                return View("Index", products.OrderBy(o=>o.Name).ToList());
            }
           
        }
        //
        // GET: /DataAccess/Products/Details/5

        public ActionResult Details(int id = 0)
        {
            Product product = db.Products.Single(p => p.ProductID == id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /DataAccess/Products/Create

        public ActionResult Create()
        {
            ViewBag.ProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name");
            ViewBag.ProductModelID = new SelectList(db.ProductModels, "ProductModelID", "Name");
            return View();
        }

        //
        // POST: /DataAccess/Products/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.AddObject(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name", product.ProductCategoryID);
            ViewBag.ProductModelID = new SelectList(db.ProductModels, "ProductModelID", "Name", product.ProductModelID);
            return View(product);
        }

        //
        // GET: /DataAccess/Products/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Product product = db.Products.Single(p => p.ProductID == id);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.ProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name", product.ProductCategoryID);
            ViewBag.ProductModelID = new SelectList(db.ProductModels, "ProductModelID", "Name", product.ProductModelID);
            return View(product);
        }

        //
        // POST: /DataAccess/Products/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Attach(product);
                db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name", product.ProductCategoryID);
            ViewBag.ProductModelID = new SelectList(db.ProductModels, "ProductModelID", "Name", product.ProductModelID);
            return View(product);
        }

        //
        // GET: /DataAccess/Products/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Product product = db.Products.Single(p => p.ProductID == id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // POST: /DataAccess/Products/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Single(p => p.ProductID == id);
            db.Products.DeleteObject(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}